package com.thoughtworks.wechatmoments.logic.moments;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.thoughtworks.commonlibrary.compat.Image.ImageLoader;
import com.thoughtworks.wechatmoments.R;
import com.thoughtworks.wechatmoments.entity.BaseEntity;
import com.thoughtworks.wechatmoments.entity.TweetsEntity;
import com.thoughtworks.wechatmoments.entity.TweetsEntity.CommentsBean;
import com.thoughtworks.wechatmoments.entity.TweetsEntity.ImagesBean;
import com.thoughtworks.wechatmoments.entity.TweetsEntity.SenderBean;
import com.thoughtworks.wechatmoments.entity.UserInfoEntity;
import com.thoughtworks.wechatmoments.view.FriendGridLayoutManager;
import com.thoughtworks.wechatmoments.widget.FriendLinearLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * desc   : 微博的adapter
 * author : guowenlong
 * Email  : guowenlong20000@gmail.com
 * time   : 2018年12月05日16 : 18
 */
public class MomentsRecycleViewAdapter extends RecyclerView.Adapter {
    public final static int TYPE_USERINFO = 1;
    public final static int TYPE_TWEETS = 2;
    private List<BaseEntity> mList;

    public MomentsRecycleViewAdapter() {
        this.mList = new ArrayList<>();
        UserInfoEntity userInfoEntity = new UserInfoEntity();
        userInfoEntity.setType(TYPE_USERINFO);
        mList.add(userInfoEntity);
    }

    public void addTweetsList(@NonNull List<TweetsEntity> list) {
        this.mList.addAll(list);
    }

    public void refreshTweets(@NonNull List<TweetsEntity> list) {
        ArrayList<BaseEntity> userInfoEntities = new ArrayList<>();
        userInfoEntities.add(mList.get(0));
        mList = userInfoEntities;
        mList.addAll(list);
    }

    public void setUserInfo(@NonNull UserInfoEntity userinfo) {
        ((UserInfoEntity) mList.get(0)).update(userinfo.getProfileimage(), userinfo.getAvatar(), userinfo.getNick(), userinfo.getUsername());
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int type) {
        switch (type) {
            case MomentsRecycleViewAdapter.TYPE_TWEETS:
                return new TweetsViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_tweets, viewGroup, false));
            case MomentsRecycleViewAdapter.TYPE_USERINFO:
                return new UserInfoViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_userinfo, viewGroup, false));
            default:
        }
        return new TweetsViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_tweets, viewGroup, false));
    }

    @Override
    public int getItemViewType(int position) {
        return mList.get(position).getType();
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        if (viewHolder instanceof UserInfoViewHolder) {
            UserInfoViewHolder userinfoViewHolder = (UserInfoViewHolder) viewHolder;
            UserInfoEntity userInfoEntity = (UserInfoEntity) mList.get(i);
            if (userInfoEntity != null) {
                userinfoViewHolder.mUsername.setText(userInfoEntity.getUsername());
                ImageLoader.imageLoad(userinfoViewHolder.itemView.getContext(), userInfoEntity.getAvatar(), userinfoViewHolder.mAvatar, R.mipmap.ic_launcher);
                ImageLoader.backgroundLoad(userinfoViewHolder.itemView.getContext(), userInfoEntity.getProfileimage(), userinfoViewHolder.mBackground, R.mipmap.ic_launcher);
            }
        } else if (viewHolder instanceof TweetsViewHolder) {
            TweetsViewHolder tweetsViewHolder = (TweetsViewHolder) viewHolder;
            TweetsEntity tweetsEntity = (TweetsEntity) mList.get(i);
            SenderBean sender = tweetsEntity.getSender();
            if (sender != null) {
                tweetsViewHolder.mName.setVisibility(View.VISIBLE);
                tweetsViewHolder.mName.setText(sender.getUsername());
                ImageLoader.imageLoad(tweetsViewHolder.itemView.getContext(), tweetsEntity.getSender().getAvatar(), tweetsViewHolder.mAvatar, R.mipmap.ic_launcher);
            } else {
                tweetsViewHolder.mName.setVisibility(View.GONE);
            }
            String content = tweetsEntity.getContent();
            if (TextUtils.isEmpty(content)) {
                tweetsViewHolder.mContent.setVisibility(View.GONE);
            } else {
                tweetsViewHolder.mContent.setText(tweetsEntity.getContent());
                tweetsViewHolder.mContent.setVisibility(View.VISIBLE);
            }
            List<ImagesBean> images = tweetsEntity.getImages();
            if (images != null) {
                ArrayList<String> imageUrls = new ArrayList<>();
                for (ImagesBean image : images) {
                    imageUrls.add(image.getUrl());
                }
                tweetsViewHolder.mImages.setVisibility(View.VISIBLE);
                tweetsViewHolder.mImages.setLayoutManager(getGridLayoutManager(viewHolder.itemView.getContext(),imageUrls));
                tweetsViewHolder.mImages.setAdapter(new ImagesAdapter(imageUrls));
            } else {
                tweetsViewHolder.mImages.setVisibility(View.GONE);
            }
            List<CommentsBean> comments = tweetsEntity.getComments();
            if (comments != null && comments.size() != 0) {
                tweetsViewHolder.mComments.setAdapter(new CommentsAdapter(comments));
                tweetsViewHolder.mComments.setVisibility(View.VISIBLE);
            } else {
                tweetsViewHolder.mComments.setVisibility(View.GONE);
            }
        }
    }

    private GridLayoutManager getGridLayoutManager(Context context, @NonNull List<String> imageUrls) {
        GridLayoutManager friendGridLayoutManager = new GridLayoutManager(context, 6);
        friendGridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int i) {
                switch (imageUrls.size()) {
                    case 1:
                        return 6;
                    case 4:
                        return 3;
                    default:
                        return 2;
                }
            }
        });
        return friendGridLayoutManager;
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    static class TweetsViewHolder extends RecyclerView.ViewHolder {
        private TextView mName;
        private ImageView mAvatar;
        private TextView mContent;
        private RecyclerView mImages;
        private RecyclerView mComments;

        TweetsViewHolder(@NonNull View itemView) {
            super(itemView);
            mName = itemView.findViewById(R.id.tv_name);
            mAvatar = itemView.findViewById(R.id.iv_avatar);
            mContent = itemView.findViewById(R.id.tv_content);
            mImages = itemView.findViewById(R.id.fl_images);
            mComments = itemView.findViewById(R.id.rv_comments);
        }
    }

    static class UserInfoViewHolder extends RecyclerView.ViewHolder {
        private TextView mUsername;
        private ImageView mAvatar;
        private LinearLayout mBackground;

        UserInfoViewHolder(@NonNull View itemView) {
            super(itemView);
            mUsername = itemView.findViewById(R.id.tv_username);
            mAvatar = itemView.findViewById(R.id.iv_user_avatar);
            mBackground = itemView.findViewById(R.id.ll_background);
        }
    }
}